﻿Imports MySql.Data.MySqlClient
Public Class t02_master_sopir
    Inherits DevExpress.XtraEditors.XtraForm
    Dim acr As New ModLibrary.All_Function_Cls
    Public BS As BindingSource

    Function Form_Valid()
        Dim Pesan, Validasi As String
        Pesan = "" : Validasi = ""

        If PLAT_NOMOR.Text = "" Then Pesan += "- Plat Nomor Tidak Boleh Kosong " & vbNewLine
        If NAMA.Text = "" Then Pesan += "- Nama Tidak Boleh Kosong " & vbNewLine
        If ALAMAT.Text = "" Then Pesan += "- Alamat Tidak Boleh Kosong " & vbNewLine
        If NO_TELP.Text = "" Then Pesan += "- No_Telp Tidak Boleh Kosong " & vbNewLine
        If Pesan <> "" Then
            Validasi = "Gagal menyimpan data : " & vbNewLine & vbNewLine & Pesan
        End If
        Return Validasi
    End Function

    Private Sub t02_master_sopir_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.UserSkins.OfficeSkins.Register()
        Me.LookAndFeel.SkinName = "Office 2010 Blue"
        DefaultLookAndFeel1.LookAndFeel.SkinName = "Office 2010 Blue"
        Me.Master_trukTableAdapter1.Fill(Me.DS_t02_master_sopir.master_truk)
        LoadNow()
        acr.MoveNavi("Add", 0, Me, BS)
    End Sub

    Sub LoadNow()
        BS = Master_sopirBindingSource
        Me.Master_sopirTableAdapter.Fill(Me.DS_t02_master_sopir.master_sopir)
        BS.MoveLast()

    End Sub

    Private Sub ID_TRUKComboBox_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)

        ' On Error Resume Next
        ' Draw the default background
        'e.DrawBackground()
        ' The ComboBox is bound to a DataTable,
        ' so the items are DataRowView objects.
        'Dim drv As DataRowView = CType(ID_TRUKComboBox.Items(e.Index), DataRowView)
        ' Retrieve the value of each column.
        ' Dim id1 As String = drv("ID_TRUK").ToString()
        'Dim id2 As String = drv("PLAT_NOMOR").ToString()

        ' +++++++++++++++++++++++++++++++++++++++++
        ' Get the bounds for the first column
        ' Draw the text on the first column
        'Using sb As SolidBrush = New SolidBrush(e.ForeColor)
        '     e.Graphics.DrawString(id1, e.Font, sb, r1)
        'End Using
        ' Draw a line to isolate the columns
        ' Using p1 As Pen = New Pen(Color.Gainsboro)
        'e.Graphics.DrawLine(p1, r1.Right - 2, 0, r1.Right - 2, r1.Bottom - 2)
        ' End Using
        'ID_TRUKComboBox.Text = id2
        ' +++++++++++++++++++++++++++++++++++++++++
        ' Get the bounds for the second column
        'Dim r2 As Rectangle = e.Bounds
        'r2.X = e.Bounds.Width * 0.2
        'r2.Width = r2.Width * 0.8
        ' Draw the text on the second column
        'Using sb As SolidBrush = New SolidBrush(e.ForeColor)
        'e.Graphics.DrawString(id2, e.Font, sb, r2)
        'End Using
        ' Draw a line to isolate the columns

    End Sub

    Sub Save()
        Try
            If Form_Valid() = "" Then
                Me.Master_sopirBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DS_t02_master_sopir)
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Save_Special()
        Try
            Me.Validate()
            Me.Master_sopirBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DS_t02_master_sopir)
            MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
            LoadNow()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Delete()
        If MsgBox("Delete this data ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Confirmation") = MsgBoxResult.Yes Then
            BS.RemoveCurrent()
            If MsgBox("if you are sure... click [Yes] button to delete and save this changes..", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "Pending Delete Data") = MsgBoxResult.Ok Then
                If Form_Valid() = "" Then
                    Save()
                Else
                    Save_Special()
                End If
            Else
                acr.MoveNavi("Load", 0, Me, BS)
            End If
        End If
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click, Btn_Refresh.Click, Btn_Previous.Click, Btn_Next.Click, Btn_Last.Click, Btn_First.Click, Btn_Delete.Click, Btn_Add.Click
        Select Case CType(sender, Button).Name.ToString
            Case "Btn_Save"
                Save()
            Case "Btn_Refresh"
                acr.MoveNavi("Load", 0, Me, BS)
            Case "Btn_Previous"
                acr.MoveNavi("MovePrev", 0, Me, BS)
            Case "Btn_Next"
                acr.MoveNavi("MoveNext", 0, Me, BS)
            Case "Btn_Last"
                acr.MoveNavi("MoveLast", 0, Me, BS)
            Case "Btn_First"
                acr.MoveNavi("MoveFirst", 0, Me, BS)
            Case "Btn_Add"
                acr.MoveNavi("Add", 0, Me, BS)
            Case "Btn_Delete"
                Delete()
        End Select
    End Sub

    Private Sub Master_sopirDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Master_sopirDataGridView.CellClick
        ID_SOPIR.Text = Master_sopirDataGridView.Rows(e.RowIndex).Cells(0).Value.ToString()
        PLAT_NOMOR.Text = Master_sopirDataGridView.Rows(e.RowIndex).Cells(1).Value.ToString()
        NAMA.Text = Master_sopirDataGridView.Rows(e.RowIndex).Cells(3).Value.ToString()
        ALAMAT.Text = Master_sopirDataGridView.Rows(e.RowIndex).Cells(4).Value.ToString()
        NO_TELP.Text = Master_sopirDataGridView.Rows(e.RowIndex).Cells(5).Value.ToString()
    End Sub
    Sub clearForm()
        ID_SOPIR.Text = ""
        PLAT_NOMOR.Text = ""
        NAMA.Text = ""
        ALAMAT.Text = ""
        NO_TELP.Text = ""
    End Sub
    Private Sub Master_sopirDataGridView_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles Master_sopirDataGridView.DataError
        e.Cancel = True
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Master_sopirDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Master_sopirDataGridView.CellContentClick

    End Sub

    Private Sub TextBox1_TextChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NAMA.TextChanged

    End Sub

    Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SIMPAN_BTN.Click
        Dim myConnString As String
        myConnString = "server=localhost;User Id=root;password=;Persist Security Info=True;database=db_petruk_new"
        Dim ds As New DataSet
        Dim dsconn As New MySqlConnection(myConnString)
        Dim strsql As String = ""

        Try
            If Form_Valid() = "" Then
                If ID_SOPIR.Text = "" Or ID_SOPIR.Text = "-1" Then
                    strsql = "INSERT INTO master_sopir (ID_TRUK,NAMA,ALAMAT,NO_TELP) VALUES ('" & PLAT_NOMOR.SelectedValue.ToString & "','" & NAMA.Text & "','" & ALAMAT.Text & "','" & NO_TELP.Text & "')"
                Else
                    strsql = "UPDATE master_sopir SET ID_TRUK = '" & PLAT_NOMOR.SelectedValue.ToString & "', NAMA = '" & NAMA.Text & "',ALAMAT = '" & ALAMAT.Text & "',NO_TELP = '" & NO_TELP.Text & "' WHERE ID_SOPIR = '" & ID_SOPIR.Text & "'"
                End If
                Dim dsDa As New MySqlCommand(strsql, dsconn)
                dsconn.Open()
                dsDa.ExecuteNonQuery()
                Me.Validate()
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
                clearForm()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        LoadNow()
    End Sub

    Private Sub TAMBAH_BTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TAMBAH_BTN.Click
        clearForm()
    End Sub
End Class